package grow

import (
	"github.com/hwcer/cosgo/slice"
	"github.com/hwcer/yyds/context"
	"github.com/hwcer/yyds/errors"
	"server/game/cache"
	"server/game/model"
	"server/share/config"
)

/**
 * @name setDeadList
 * @param string ID unit唯一id
 * 设置死亡名单,或者从流亡之地取出来
 */

func (this *unit) SetDeadList(c *context.Context) any {
	ID := c.GetString("ID")
	if ID == "" {
		return errors.ErrArgEmpty
	}
	items := cache.GetItems(c.Player.Updater)
	data := items.Get(ID)
	if data == nil {
		return errors.ErrDataNotExists
	}
	death := &model.UnitDeath{}
	if err := data.Attach.Unmarshal(model.AttachUnitDeath, death); err != nil {
		return err
	}
	if death.Status == 1 {
		return nil
	} else if death.Status == 2 {
		death.Value += int32(c.Milli() - death.Time)
		death.Time = 0
	}
	death.Status = 1
	items.SetAttach(data, model.AttachUnitDeath, death)
	return nil
}

/**
 * @name setDeadArea
 * @param string ID 唯一id
 * 设置放逐之地
 */
func (this *unit) SetDeadArea(c *context.Context) any {
	ID := c.GetString("ID")
	if ID == "" {
		return errors.ErrArgEmpty
	}
	items := cache.GetItems(c.Player.Updater)
	data := items.Get(ID)
	if data == nil {
		return errors.ErrDataNotExists
	}
	death := &model.UnitDeath{}
	if err := data.Attach.Unmarshal(model.AttachUnitDeath, death); err != nil {
		return err
	}
	if death.Status == 0 {
		return c.Errorf(0, "角色未死亡")
	}
	if death.Status == 2 {
		return nil
	}
	death.Status = 2
	death.Time = c.Milli()
	items.SetAttach(data, model.AttachUnitDeath, death)
	return nil
}

/**
* @name revive
* @param string ID
* 复活人物
 */
func (this *unit) Revive(c *context.Context) any {
	ID := c.GetString("ID")
	if ID == "" {
		return errors.ErrArgEmpty
	}
	items := cache.GetItems(c.Player.Updater)
	data := items.Get(ID)
	if data == nil {
		return errors.ErrDataNotExists
	}
	death := &model.UnitDeath{}
	if err := data.Attach.Unmarshal(model.AttachUnitDeath, death); err != nil {
		return err
	}
	if death.Status == 0 {
		return c.Errorf(0, "角色未死亡")
	}
	v := death.Value
	if death.Status == 2 {
		v += int32(c.Milli() - death.Time)
	}
	unitConfig := config.Data.Unit[data.IID]
	if unitConfig == nil {
		return errors.ErrConfigEmpty
	}
	if v < unitConfig.ResurrectionTime*1000 {
		return c.Errorf(0, "时间未到")
	}
	death = nil
	items.SetAttach(data, model.AttachUnitDeath, death)
	return nil
}

/**
 * @name boostUnit
 * @param string unitID
 * @param int itemID
 * @param int num
 * 复活加速
 */

func (this *unit) BoostUnit(c *context.Context) any {
	unitID := c.GetString("unitID")
	if unitID == "" {
		return errors.ErrArgEmpty
	}
	items := cache.GetItems(c.Player.Updater)
	data := items.Get(unitID)
	if data == nil {
		return errors.ErrDataNotExists
	}
	itemID := c.GetInt32("itemID")
	num := c.GetInt32("num")
	if itemID <= 0 || num <= 0 {
		return errors.ErrArgEmpty
	}

	death := &model.UnitDeath{}
	if err := data.Attach.Unmarshal(model.AttachUnitDeath, death); err != nil {
		return err
	}
	if death.Status != 2 {
		return c.Errorf(0, "不在复活中")
	}

	speedUpCostItem := config.Data.Base["speedUpCostItem"]
	arr := slice.Multiple(speedUpCostItem.StrVal, ",", ":")
	var subTime int32
	for _, v := range arr {
		if len(v) >= 2 && v[0] == itemID {
			subTime = v[1]
			break
		}
	}
	if subTime == 0 {
		return c.Errorf(0, "消耗配置不存在")
	}

	death.Value += subTime * num * 1000

	items.SetAttach(data, model.AttachUnitDeath, death)
	return nil
}
